package xin.printk.android.module.guide;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import xin.printk.android.MainActivity;
import xin.printk.android.R;
import xin.printk.android.utils.AppConstants;
import xin.printk.android.utils.PrefUtils;
import xin.printk.android.view.adapter.ImagePagerAdapter;

public class GuideActivity extends AppCompatActivity {

    private Button mStartButton;
    private List<ImageView> mImageList;
    private int mCurrentPageIndex = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guide);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.hide();
            }
        }
        mStartButton = findViewById(R.id.bt_start);
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrefUtils.putBoolean(AppConstants.FIRST_START_UP, false);
                startActivity(new Intent(GuideActivity.this, MainActivity.class));
                finish();
            }
        });
        ViewPager pager = findViewById(R.id.vp_guide_page);
        getImageList();
        PagerAdapter adapter = new ImagePagerAdapter(mImageList);
        pager.setAdapter(adapter);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurrentPageIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE && mCurrentPageIndex == mImageList.size() - 1) {
                    mStartButton.setVisibility(View.VISIBLE);
                } else {
                    mStartButton.setVisibility(View.GONE);
                }
            }
        });
    }

    void getImageList() {
        mImageList = new ArrayList<>();
        ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        ImageView image1 = new ImageView(this);
        image1.setLayoutParams(lp);
        image1.setScaleType(ImageView.ScaleType.FIT_XY);
        image1.setImageResource(R.mipmap.guide_image1);
        mImageList.add(image1);

        ImageView image2 = new ImageView(this);
        image2.setLayoutParams(lp);
        image2.setScaleType(ImageView.ScaleType.FIT_XY);
        image2.setImageResource(R.mipmap.guide_image2);
        mImageList.add(image2);

        ImageView image3 = new ImageView(this);
        image3.setLayoutParams(lp);
        image3.setScaleType(ImageView.ScaleType.FIT_XY);
        image3.setImageResource(R.mipmap.guide_image3);
        mImageList.add(image3);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            );

        }
    }
}
